package com.boot.unity.api.wx.service;

import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.wx.entity.TrainUserHt;
import com.boot.unity.api.wx.utils.IpAddress;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class WxClassTypeService {

    @Value("${serv.servurl}")
    private String servurl;

    public Object queryHtByUserIdAndClassId(String userId, String classId) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/classType/queryHtByUserIdAndClassId?userId=" + userId + "&classId=" + classId;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object saveUserHt(TrainUserHt trainUserHt) {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("trainUserHt", trainUserHt);
        //设置请求头和数据
        RequestBody body = RequestBody.create(mediaType,jsonObject.toString());
        //设置地址，方法，数据格式
        Request request = new Request.Builder()
                .url(servurl+ "/wx/classType/saveUserHt")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (IOException e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object list(String val) {
        OkHttpClient client = new OkHttpClient();
        String url = servurl+ "/wx/classType/list?val=" + val;
        Request request = new Request.Builder()
                .url(url)
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }
}
